/** 处理单个格式数据类型 */
export interface IFormCommonData {
    key: string;//KEY
    value: any;//值
    title: any;//显示的标题
    disable: boolean;//是否禁用
    show: boolean;//是否显示
    type?: FormItemType,//类型 TEXT + COLOR(字段必包含Color)
}

/** 处理多种格式类型的数据 */
export interface IFormCommonMultData extends IFormCommonData {
    //处理 SELECT TREE...
    selectList?: Array<ISelectData>
    srcList?: Array<ISelectData>
    selectValue?: any,
    /** 是否多选 */
    isMultiplay?: boolean
}

export interface ISelectModel {
    /** 要做成选择框的字段 */
    filed: string
    /** 要设置的唯一Id 字段名 */
    key: string,//KEY 字段名
    /** 要显示的名称的字段名 */
    value: string,//VALUE 字段名
    /** 获取选择框数据方法 */
    getSelectFunc: Function
    /** 是否多选 */
    isMultiplay?: boolean
}
export interface ISelectData {
    key: string
    value: any
}

export enum FormItemType {
    TEXT = 'text',
    COLOR = 'color',
    SELECT = 'select',
    CHECKBOX = 'checkbox',
}

export enum TouchType {
    SEE = "see",
    EDIT = 'edit',
    DELETE = 'delete',
    ADD = 'add'
}

//设备的操作类型
export enum DeviceTouchType {
    SEE = "see",
    EDIT = 'edit',
    DELETE = 'delete',
    ADD = 'add',

    VOICE_SETTING = "voiceSetting",
    SHOW_SETTING = "showSetting",
    REFRESH = "refresh",
    SCREENSHOTS = "screenshots",
    
    REBOOT = "reboot",
    SHUTDOWN = "shutdown",
    UPDATE_APK = "updateApk",
    SET_AUTO_OPEN="setAutoOpen",
}

export enum SysUserTouchType {
    SEE = "see",
    EDIT = 'edit',
    DELETE = 'delete',
    ADD = 'add',
    RoleAllocation = "role_allocation"
}

export class FormDataUtils {
    /**
     * 获取数据
     * @param row 要显示的数据对象
     * @param langKey i18n 对应的key
     * @param t $t 对象
     * @param unDisableArr 要禁用显示的字段集合
     * @param noShowArr 不显示的字段集合
     * @param type 类型
     * @returns 返回转换后的数据：Array<IFormCommonData>
     */
    public static getForms(row: any, langKey: string, t: any, unDisableArr: Array<string> = [], noShowArr: Array<string> = [], type: FormItemType = FormItemType.TEXT): Array<IFormCommonData> {
        let arr = [];
        for (let key in row) {
            let strKey = `${langKey}.${key}`;
            let v: IFormCommonData = {
                key: key,
                title: t(strKey),
                show: noShowArr.indexOf(key) == -1,
                disable: unDisableArr.indexOf(key) > -1,
                value: row[key],
                type: type == FormItemType.COLOR ? key.indexOf('Color') > -1 ? type : FormItemType.TEXT : type,
            }
            arr.push(v);
        }
        return arr
    }

    /**
    * 获取数据
    * @param row 要显示的数据对象
    * @param langKey i18n 对应的key
    * @param t $t 对象
    * @param unDisableArr 要禁用显示的字段集合
    * @param noShowArr 不显示的字段集合
    * @param type 类型
    * @param selectFiles select 选择框的字段
    * @returns 返回转换后的数据：Array<IFormCommonData>
    */
    public static async getFormsMultiplay(row: any, langKey: string, t: any,
        unDisableArr: Array<string> = [], noShowArr: Array<string> = [], selectFiles: Array<ISelectModel> = []): Promise<Array<IFormCommonMultData>> {
        let arr: Array<IFormCommonMultData> = new Array();
        console.error('v')
        for (let key in row) {
            let strKey = `${langKey}.${key}`;
            let vType = FormItemType.TEXT;
            let v: IFormCommonMultData = {
                key: key,
                title: t(strKey),
                show: noShowArr.indexOf(key) == -1,
                disable: unDisableArr.indexOf(key) > -1,
                value: row[key],
                type: vType,
            }
            for (let i = 0; i < selectFiles.length; i++) {
                if (selectFiles[i].filed == key) {
                    v.type = FormItemType.SELECT;
                    let result = await selectFiles[i].getSelectFunc();
                    let selectArr: Array<ISelectData> = [];
                    for (let j = 0; j < result.length; j++) {
                        let d: ISelectData = { key: result[j][selectFiles[i].key], value: result[j][selectFiles[i].value] };
                        selectArr.push(d);
                    }
                    v.selectList = selectArr
                    v.srcList = selectArr
                    v.isMultiplay = selectFiles[i].isMultiplay
                    if (v.isMultiplay) {
                        v.selectValue = row[key] ? row[key].split(",") : []
                    } else {
                        v.selectValue = row[key] ? row[key] : ""
                    }
                    break;
                }
            }
            arr.push(v);
        }
        return arr
    }

    /**
     * 转为Object 对象
     * @param forms 要转为Object对象的数据
     */
    public static getFormObject(forms: Array<IFormCommonData>): any {
        let obj: any = {};
        for (let i = 0; i < forms.length; i++) {
            obj[forms[i].key] = forms[i].value;
        }
        return obj;
    }

    /**
     * 转为Object 对象
     * @param forms 要转为Object对象的数据
     */
    public static getFormMultiplayObject(forms: Array<IFormCommonMultData>): any {
        let obj: any = {};
        for (let i = 0; i < forms.length; i++) {
            obj[forms[i].key] = forms[i].value;
            if (forms[i].type == FormItemType.SELECT) {
                let o = forms[i].selectValue
                if (o && o['join']) {
                    obj[forms[i].key] = forms[i].selectValue?.join(",");
                } else {
                    obj[forms[i].key] = forms[i].selectValue;
                }
            }
        }
        return obj;
    }

}